//
//  FaceFiltratePushViewController.m
//  Ai
//
//  Created by 杜超男 on 2019/3/6.
//  Copyright © 2019年 杜超男. All rights reserved.
//

#import "FaceFiltratePushViewController.h"
#import "Masonry.h"
#import "headerView.h"
#import "FaceFiltrateTableViewCell.h"
#import "SentViewController.h"
#import "UnsentViewController.h"
#import "BRPickerView.h"
@interface FaceFiltratePushViewController ()<UITableViewDelegate , UITableViewDataSource>
{
    NSDateFormatter *Dateformatter;
    NSString *nyrzString;
    NSString *dateStr_yyyy;
    NSString *dateStr_MM;
    NSString *dateStr_dd;
    NSString *dateStr_EEEE;
    UILabel *TimeLabel;
    NSMutableDictionary *showDic;//用来判断分组展开与收缩的
    NSString *key;
    NSString *namensstring;
}
@property (nonatomic , strong)NSDateFormatter *formatter;
@property (nonatomic , strong)NSDate *currentDate;
@property (nonatomic , strong)UITableView *tableView;//
@property (nonatomic , strong)UITableView *cellTableView;
@property (nonatomic , strong)NSMutableArray *rowMuarr;//row数据数组
@property (nonatomic , strong)NSMutableArray *sectionMuarr;//section数据数组
\
@property (nonatomic , strong)NSMutableArray *ClassNameArr;//学生姓名数据数组
@property (nonatomic , strong)NSMutableArray *UserTypeArr;//用户类型数据数组
@property (nonatomic , strong)NSMutableArray *StateArr;//状态数据数组

@end

@implementation FaceFiltratePushViewController
- (void)date_yyyy{
    //获取年月日
    [Dateformatter setDateFormat:@"yyyy"];
    dateStr_yyyy = [Dateformatter stringFromDate:self.currentDate];
}
- (void)date_MM{
    [Dateformatter setDateFormat:@"MM"];
    //    self.currentDate = [NSDate date];
    dateStr_MM = [Dateformatter stringFromDate:self.currentDate];
}
- (void)date_dd{
    [Dateformatter setDateFormat:@"dd"];
    //    self.currentDate = [NSDate date];
    dateStr_dd = [Dateformatter stringFromDate:self.currentDate];
}
- (void)date_EEEE{
    [Dateformatter setDateFormat:@"EEEE"];
    //    self.currentDate = [NSDate date];
    dateStr_EEEE = [Dateformatter stringFromDate:self.currentDate];
    if ([dateStr_EEEE isEqualToString:@"Monday"]) {
        dateStr_EEEE = [[NSString alloc] init];
        dateStr_EEEE = @"周一";
    }else if ([dateStr_EEEE isEqualToString:@"Tuesday"]){
        dateStr_EEEE = [[NSString alloc] init];
        dateStr_EEEE = @"周二";
    }else if ([dateStr_EEEE isEqualToString:@"Wednesday"]){
        dateStr_EEEE = [[NSString alloc] init];
        dateStr_EEEE = @"周三";
    }else if ([dateStr_EEEE isEqualToString:@"Thursday"]){
        dateStr_EEEE = [[NSString alloc] init];
        dateStr_EEEE = @"周四";
    }else if ([dateStr_EEEE isEqualToString:@"Friday"]){
        dateStr_EEEE = [[NSString alloc] init];
        dateStr_EEEE = @"周五";
    }else if ([dateStr_EEEE isEqualToString:@"Saturday"]){
        dateStr_EEEE = [[NSString alloc] init];
        dateStr_EEEE = @"周六";
    }else if ([dateStr_EEEE isEqualToString:@"Sunday"]){
        dateStr_EEEE = [[NSString alloc] init];
        dateStr_EEEE = @"周日";
    }
}
- (void)date_nyr{
    nyrzString = [NSString stringWithFormat:@"%@-%@-%@ %@",dateStr_yyyy , dateStr_MM , dateStr_dd , dateStr_EEEE];
    NSLog(@"+++++++++++++%@",nyrzString);
    TimeLabel.text = nyrzString;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    Dateformatter = [[NSDateFormatter alloc] init];
    self.ClassNameArr = [NSMutableArray arrayWithObjects:@"李建" , @"刘依依" , @"易烊千玺" , @"易烊千玺" , @"王俊凯", nil];
    self.UserTypeArr = [[NSMutableArray alloc] initWithObjects:@"VIP" , @"VIP" , @"VIP" , @"VIP" , @"普通", nil];
    self.StateArr = [[NSMutableArray alloc] initWithObjects:@"未发送" , @"未发送" , @"未发送" , @"已发送" , @"已发送", nil];
    self.currentDate = [[NSDate alloc] init];
    [self headerView];
    [self date_yyyy];
    [self date_MM];
    [self date_dd];
    [self date_EEEE];
    [self date_nyr];
    //////////
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.view.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(10, 10)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.view.bounds;
    maskLayer.path = maskPath.CGPath;
    self.view.layer.mask = maskLayer;
    
    
    showDic = [[NSMutableDictionary alloc]init];
    self.rowMuarr = [[NSMutableArray alloc] init];
    self.sectionMuarr = [[NSMutableArray alloc] initWithObjects:@"小小班", @"小小班" , @"小小班" , nil];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, self.view.bounds.size.height - 50) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    self.tableView.tableFooterView = [[UIView alloc] init];
    _sectionMuarr = [NSMutableArray arrayWithObjects:@"小小班" , @"小小班" , @"小小班" , nil];
    _rowMuarr = [NSMutableArray arrayWithObjects:@"小小班" , @"小小班" , @"小小班", nil];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (tableView == _cellTableView) {
        return 1;
    }else{
        return [_sectionMuarr count];
    }
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == _cellTableView) {
        return self.ClassNameArr.count;
    }else{
        return 1;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == _cellTableView) {
        FaceFiltrateTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (!cell) {
            cell = [[FaceFiltrateTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        }
        //rgba(247/255.0, 247, 247, 1)
        cell.backgroundColor = [UIColor colorWithRed:247/255.0 green:247/255.0 blue:247/255.0 alpha:1];
        self.cellTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        cell.NameL.text = self.ClassNameArr[indexPath.row];
        cell.TypeL.text = self.UserTypeArr[indexPath.row];
        [cell.StateB setTitle:self.StateArr[indexPath.row] forState:UIControlStateNormal];
        if ([cell.StateB.titleLabel.text isEqualToString:@"未发送"]) {
            //rgba(255/255.0, 114/255.0, 0/255.0, 1)
            [cell.StateB setTitleColor: [UIColor colorWithRed:255/255.0 green:114/255.0 blue:0/255.0 alpha:1] forState:UIControlStateNormal];
//            cell.StateB.textColor = [UIColor colorWithRed:255/255.0 green:114/255.0 blue:0/255.0 alpha:1];
        }
        if ([cell.StateB.titleLabel.text isEqualToString:@"已发送"]) {
            //rgba(72/255.0, 169/255.0, 54/255.0, 1)
            [cell.StateB setTitleColor: [UIColor colorWithRed:72/255.0 green:169/255.0 blue:54/255.0 alpha:1] forState:UIControlStateNormal];
            [cell.StateB addTarget:self action:@selector(SendBtn) forControlEvents:UIControlEventTouchUpInside];
//            cell.StateL.textColor = [UIColor colorWithRed:72/255.0 green:169/255.0 blue:54/255.0 alpha:1];
        }
        return cell;
    }
    if (tableView == self.tableView){
        static NSString *str = @"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
        if (cell == NULL) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
            cell.clipsToBounds = YES;
        }
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        self.cellTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 45, self.view.bounds.size.width, self.view.bounds.size.height-45) style:UITableViewStylePlain];
        self.cellTableView.delegate = self;
        self.cellTableView.dataSource = self;
        [self.cellTableView registerNib:[UINib nibWithNibName:@"FaceFiltrateTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
        [cell addSubview:_cellTableView];
        /////设置头标题      [学生姓名 用户类型    状态]
        UIView *HeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 45)];
        ///rgba(230/255.0, 230, 230, 1)
        HeaderView.backgroundColor = [UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1];
        //学生姓名
        UILabel *ClassNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 16, 60, 13)];
        ClassNameLabel.text = @"学生姓名";
        ClassNameLabel.font = [UIFont systemFontOfSize:13];
        [HeaderView addSubview:ClassNameLabel];
        //用户类型
        UILabel *UserTypeLabel = [[UILabel alloc] initWithFrame:CGRectMake(180, 16, 60, 13)];
        UserTypeLabel.text = @"用户类型";
        UserTypeLabel.font = [UIFont systemFontOfSize:13];
        [HeaderView addSubview:UserTypeLabel];
        //状态
        UILabel *StateLabel = [[UILabel alloc] initWithFrame:CGRectMake(330, 16, 60, 13)];
        StateLabel.text = @"状态";
        StateLabel.font = [UIFont systemFontOfSize:13];
        [HeaderView addSubview:StateLabel];
        //添加头标题到cell
        [cell addSubview:HeaderView];
        return cell;
    }
    return nil;
    
}
- (void)SendBtn{
    [self.navigationController pushViewController:[SentViewController new] animated:YES];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.cellTableView) {
        return 45;
    }else{
        if ([showDic objectForKey:[NSString stringWithFormat:@"%ld" , indexPath.section]]) {
            return 300;
        }
    }
    return 0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (tableView == self.cellTableView) {
        return 0.01;
    }else{
        return 50;
    }
    return 0;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (tableView == self.cellTableView) {
        
    }else{
        headerView *header = [[headerView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
        UIImageView *HomeHeaderImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 0, 30, 30)];
        HomeHeaderImage.image = [UIImage imageNamed:@"房子icon"];
        [header addSubview:HomeHeaderImage];
        header.backgroundColor = [UIColor whiteColor];
        UILabel *HeaderNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 0, 80, 30)];
        HeaderNameLabel.textAlignment = NSTextAlignmentCenter;
        HeaderNameLabel.text = _sectionMuarr[section];
        [header addSubview:HeaderNameLabel];
        //展开或收起的文字    rgba(72/255.0, 169/255.0, 54/255.0, 1)
        UILabel *ZSLabel = [[UILabel alloc] initWithFrame:CGRectMake(350, 0, 40, 30)];
        ZSLabel.textColor = [UIColor colorWithRed:72/255.0 green:169/255.0 blue:54/255.0 alpha:1];
        ZSLabel.font = [UIFont systemFontOfSize:13];
        
        [header addSubview: ZSLabel];
        //    header.groupName = _sectionMuarr[section];
        if (![showDic objectForKey:key]) {
            header.open = YES;
            ZSLabel.text = @"展开";
        }else{
            header.open = NO;
            ZSLabel.text = @"收起";
        }
        header.tag = section;
        UITapGestureRecognizer *singleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(SingleTap:)];
        [header addGestureRecognizer:singleRecognizer];
        return header;
    }
    return nil;
}
- (void)SingleTap:(UITapGestureRecognizer *)recognizer{
    NSInteger didSection = recognizer.view.tag;
    key = [NSString stringWithFormat:@"%ld",didSection];
    if (![showDic objectForKey:key]) {
        [showDic setObject:@"e" forKey:key];
    }else{
        [showDic removeObjectForKey:key];
    }
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:didSection] withRowAnimation:UITableViewRowAnimationFade];
}
- (void)headerView{
    UIView *headerview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
    headerview.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:headerview];
    [headerview mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.size.height.mas_equalTo(50);
    }];
    UIButton *lastBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 18, 40, 14)];
    [lastBtn setTitle:@"前一天" forState:UIControlStateNormal];
    lastBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [lastBtn setTitleColor:[UIColor colorWithRed:72/255.0 green:169/255.0 blue:54/255.0 alpha:1] forState:UIControlStateNormal];
    [lastBtn addTarget:self action:@selector(lastBtnMore) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:lastBtn];
    
    UIButton *agoBtn = [[UIButton alloc] initWithFrame:CGRectMake(350, 18, 40, 14)];
    [agoBtn setTitle:@"后一天" forState:UIControlStateNormal];
    agoBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [agoBtn setTitleColor:[UIColor colorWithRed:72/255.0 green:169/255.0 blue:54/255.0 alpha:1] forState:UIControlStateNormal];
    [agoBtn addTarget:self action:@selector(agoBtnMore) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:agoBtn];
    
    TimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 13, 170, 30)];
    TimeLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:TimeLabel];
    
    UIButton *rili = [[UIButton alloc] initWithFrame:CGRectMake(270, 13, 30, 30)];
    [rili setImage:[UIImage imageNamed:@"日历"] forState:UIControlStateNormal];
    [rili addTarget:self action:@selector(btnnn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:rili];
}
- (void)btnnn{
    NSDate *minDate = [NSDate br_setYear:1990 month:3 day:12];
    NSDate *maxDate = [NSDate date];
    [BRDatePickerView showDatePickerWithTitle:@"" dateType:BRDatePickerModeYMD defaultSelValue:0 minDate:minDate maxDate:maxDate isAutoSelect:YES themeColor:nil resultBlock:^(NSString *selectValue) {
        self->TimeLabel.text = selectValue;
    } cancelBlock:^{
        self->TimeLabel.text = self->nyrzString;
    }];
}
- (void)lastBtnMore{
    self.currentDate = [[NSDate alloc] initWithTimeIntervalSinceReferenceDate:([self.currentDate timeIntervalSinceReferenceDate]-24*3600)];
    dateStr_MM = [Dateformatter stringFromDate:self.currentDate];
    [self date_yyyy];
    [self date_MM];
    [self date_dd];
    [self date_EEEE];
    [self date_nyr];
}
- (void)agoBtnMore{
    self.currentDate = [[NSDate alloc] initWithTimeIntervalSinceReferenceDate:([self.currentDate timeIntervalSinceReferenceDate]+24*3600)];
    dateStr_MM = [Dateformatter stringFromDate:self.currentDate];
    [self date_yyyy];
    [self date_MM];
    [self date_dd];
    [self date_EEEE];
    [self date_nyr];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UnsentViewController *unsent = [[UnsentViewController alloc] init];
    namensstring = self.ClassNameArr[indexPath.row];
    unsent.NameStr = namensstring;
    [self.navigationController pushViewController:unsent animated:YES];
}
@end
